package com.offcn.test;

import com.offcn.bean.User;
import com.offcn.bean.User2;
import com.offcn.mapper.UserMapper;
import com.offcn.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo01 {
    static Logger logger = Logger.getLogger(Demo01.class);
    @Test
    public void test() {
//        System.out.println("哈哈");
        SqlSession sqlSession = MybatisUtils.getSession();
        // 方法一:
//        List<User> user = sqlSession.selectList("com.offcn.mapper.UserMapper.selectUser");
        //方法二:
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        logger.info("查询所有用户：");
        List<User> userList = mapper.selectUser();
        for (User user : userList) {
            System.out.println(user.toString());

        }
    }


    //通过用户id获取用户
    @Test
    public void test1() {

        SqlSession sqlSession = MybatisUtils.getSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(10);
        System.out.println(user.toString());

        }

    //通过用户名和密码获取用户
    @Test
    public void test2() {
        SqlSession sqlSession = MybatisUtils.getSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserByNameAndPwd("小黑","123");
        System.out.println(user.toString());

    }

    //通过用户名和密码获取用户(Map方法）
    @Test
    public void test3() {
        SqlSession sqlSession = MybatisUtils.getSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Map<String,String> hashMap = new HashMap<String, String>();
        hashMap.put("username","小黑");
        hashMap.put("pwd","123");
        User user = mapper.getUserByNameAndPwd1(hashMap);
        System.out.println(user.toString());
    }

    //新增一个用户
    @Test
    public void test4() {
        SqlSession sqlSession = MybatisUtils.getSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setId(14);
        user.setName("干干2");
        user.setPwd("666");
        // 插入成功返回1，失败返回0
        int i = mapper.insertUser(user);
        sqlSession.commit();  // 提交事务，不写的话，数据不会插入到数据库中
        System.out.println("状态码："+i);
    }

    //修改用户
    @Test
    public void test5() {
        SqlSession sqlSession = MybatisUtils.getSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setId(13);
        user.setName("gangan");
        user.setPwd("666");
        // 修改成功返回1，失败返回0
        int i = mapper.updateUser(user);
        sqlSession.commit();  // 提交事务，不写的话，数据不会插入到数据库中
        System.out.println("状态码："+i);
    }

    //通过用户id删除用户
    @Test
    public void test6() {

        SqlSession sqlSession = MybatisUtils.getSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.deleteUser(14);
        sqlSession.commit();
        System.out.println(i);
    }

    //通过用户名进行模糊查询
    @Test
    public void test7() {

        SqlSession sqlSession = MybatisUtils.getSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.getLike("小%");
        for (User user:users){
            // 快速打印语句sout+enter
            System.out.println(user.toString());
        }
    }

    //根据用户名进行模糊查询, 在mybatis中使用 <typeAlias type="com.offcn.bean.User" alias="user"></typeAlias>
    @Test
    public void test8() {
        SqlSession sqlSession = MybatisUtils.getSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.getLike2("小%");
        for (User user:users){
            // 快速打印语句sout+enter
            System.out.println(user.toString());
        }
    }

    //根据用户名进行模糊查询, 在mybatis中使用 使用<typeAlias type="com.offcn.bean"></typeAlias>，形成别名默认别名，所有的类名的默认别名，
    //所有别名默认类名小写
    @Test
    public void test9() {
        SqlSession sqlSession = MybatisUtils.getSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.getLike3("小%");
        for (User user:users){
            // 快速打印语句sout+enter
            System.out.println(user.toString());
        }

    }

    //User2：pssword与数据库中user1表的pwd字段不不一致，通过sql语句设置别名
    @Test
    public void test10() {
        SqlSession sqlSession = MybatisUtils.getSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User2> users = mapper.getLike4("小%");
        for (User2 user:users){
            // 快速打印语句sout+enter
            System.out.println(user.toString());
        }
    }

    //User2：pssword与数据库中user1表的pwd字段不不一致，使用结果集映射->ReslutMap
    @Test
    public void test11() {
        SqlSession sqlSession = MybatisUtils.getSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User2> users = mapper.getLike5("小%");
        for (User2 user:users){
            // 快速打印语句sout+enter
            System.out.println(user.toString());
        }
    }
}

